home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12627 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  45 lines

  1. Path: crl.crl.com!not-for-mail
  2. From: bobfry@crl.com (Robert Fry)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What is byte-alignment?
  5. Date: 1 Apr 1996 16:32:12 -0800
  6. Organization: CRL Dialup Internet Access
  7. Message-ID: <4jpsic$gri@crl.crl.com>
  8. References: <4jprfe$c6q@alcor.usc.edu>
  9. NNTP-Posting-Host: crl.com
  10.  
  11. wawda@alcor.usc.edu (Abu Wawda) writes:
  12.  
  13. >I often hear the phrase "byte-aligment" in this newsgroups, yet have
  14. >no clue what it is. Can someone please explain it to me? It comes up
  15. >often when people talk about structures. Thanks in advance,
  16.  
  17. It refers to the requirements certain machines place on access of data 
  18. from memory. (And I'm sure it shows up in other conditions, but this is 
  19. the most obvious case). As an example, many RISC CPUs can read four bytes 
  20. of data from RAM in a single bus cycle -- but only if the first byte read 
  21. has a physical address that's aligned on a four-byte boundary (i.e. the 
  22. last two bits of the address are zero).
  23.  
  24. Imagine the following structure:
  25.  
  26. struct {
  27.   char  byte;
  28.   short word;
  29.   long  longint;
  30. } foo;
  31.  
  32. Because of the above-mentioned reasons, many compilers will put the first 
  33. byte of foo (foo.byte) at a doubleword boundary. (e.g. &foo == 0x20). 
  34. Given that, C does not guarantee that &foo.word == 0x21. It might well be 
  35. put at 0x24, with foo.longint at 0x28. Other compilers will put byte, 
  36. word, and longint at 0x20, 0x21, and 0x23. You can't guarantee what byte
  37. these structures are aligned at, and you might have troubles with trying 
  38. to guess where they are when reading, say, a binary data stream. This 
  39. problem is the most common case of a byte-alignemnt problem that I've seen.
  40.  
  41. (Note that the addresses given above are just for examples. Addresses 
  42. won't necessarily look like integers.)
  43.  
  44. Bob
  45.